﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Wpf.CommonTools.WebExtension
{
    public static class WebExtension
    {     
        /// <summary>
        /// 替换Querystring中的值,如果value为空则删除该Querystring
        /// </summary>
        /// <param name="url">Url</param>
        /// <param name="queryName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToReplaceQueryString(this string url, string queryName, params object[] value)
        {
            if (string.IsNullOrEmpty(url))
            {
                return url;
            }
            if (string.IsNullOrEmpty(queryName))
            {
                return url;
            }


            string[] queryNames = Regex.Split(queryName, ",");

            if (value == null || value.Length == 0)
            {
                value = new object[queryNames.Length];
            }
            if (queryNames.Length != value.Length)
            {
                throw new ArgumentOutOfRangeException("value");
            }
            string temp = url;
            for (int i = 0; i < queryNames.Length; i++)
            {
                replaceQueryString(ref temp, true, queryNames[i], value[i]);
            }
            return temp;
        }

        private static string replaceQueryString(ref string url, bool encript, string queryName, object value)
        {
            string pattern = queryName + @"=([+-<>$#%~]|\w)*\s*\w*&{0}";
            if (value == null)
            {
                pattern = "(&|\\?)" + pattern;
                url = System.Text.RegularExpressions.Regex.Replace(url, pattern, "");
                if (url.IndexOf("?") == -1)
                {
                    int j = url.IndexOf("&");
                    if (j != -1)
                    {
                        url = (url.Substring(0, j) + "?" + url.Substring(j + 1, url.Length - j - 1));
                    }
                }
                return url;
            }
            string qValue = encript ? Microsoft.JScript.GlobalObject.escape(value.ToString()) : value.ToString();
            if (System.Text.RegularExpressions.Regex.IsMatch(url, "(&|\\?)" + pattern))
            {
                url = System.Text.RegularExpressions.Regex.Replace(url, pattern, queryName + "=" + qValue);
            }
            else
            {
                if (url.Contains("?"))
                {
                    url += "&" + queryName + "=" + qValue;
                }
                else
                {
                    url += "?" + queryName + "=" + qValue;
                }
            }
            return url;
        }

        /// <summary>
        /// Javascript ESCAPE
        /// </summary>
        /// <param name="string">转换指定字符串</param>
        /// <returns></returns>
        public static string ToEscape(this object @string)
        {
            if (@string == null)
            {
                return null;
            }
            else
            {
                return Microsoft.JScript.GlobalObject.escape(@string);
            }
        }
        /// <summary>
        /// Javascript UNESCAPE
        /// </summary>
        /// <param name="string">转换指定字符串</param>
        /// <returns></returns>
        public static string ToUnEscape(this string @string)
        {
            if (@string == null)
            {
                return null;
            }
            else
            {
                return Microsoft.JScript.GlobalObject.unescape(@string);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="encodeNonAsciiCharacters">是否转换英文字符</param>
        /// <returns></returns>
        public static string ToUnicode(this string str, bool encodeNonAsciiCharacters = true)
        {
            StringBuilder sb = new StringBuilder();
            if (!string.IsNullOrEmpty(str))
            {
                if (encodeNonAsciiCharacters == true)
                {
                    foreach (char c in str)
                    {
                        sb.Append("\\u" + ((int)c).ToString("x"));
                    }
                }
                else
                {
                    foreach (char c in str)
                    {
                        if (c > 127)
                        {
                            sb.Append("\\u" + ((int)c).ToString("x"));
                        }
                        else
                        {
                            sb.Append(c);
                        }
                    }
                }
            }
            return sb.ToString();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string FromUnicode(this string str)
        {
            return Regex.Replace(str,
                @"\\u(?<Value>[a-zA-Z0-9]{4})",
                m =>
                {
                    return ((char)int.Parse(m.Groups["Value"].Value, NumberStyles.HexNumber)).ToString();
                });
        }

    }
}
